package org.dromara.wms.service;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.WmsProcess;
import org.dromara.wms.mapper.WmsProcessMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工序管理Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-05-01
 */
@RequiredArgsConstructor
@Service
public class WmsProcessService extends BaseService {

    private final WmsProcessMapper baseMapper;

    /**
     * 查询工序管理
     */
    public WmsProcess queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询工序管理列表
     */
    public TableDataInfo<WmsProcess> queryPageList(WmsProcess bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WmsProcess> lqw = buildQueryWrapper(bo);
        Page<WmsProcess> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工序管理列表
     */
    public List<WmsProcess> queryList(WmsProcess bo) {
        LambdaQueryWrapper<WmsProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<WmsProcess> buildQueryWrapper(WmsProcess bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsProcess> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), WmsProcess::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getTreeValue()), WmsProcess::getTreeValue, bo.getTreeValue());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WmsProcess::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增工序管理
     */
    public Boolean insertByBo(WmsProcess bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改工序管理
     */
    public Boolean updateByBo(WmsProcess bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工序管理
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
